package web;

import pojo.Cart;
import pojo.Order;
import pojo.OrderItem;
import pojo.User;
import service.OrderService;
import service.impl.OrderServiceImpl;
import utils.JDBCUtils;
import utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    /**
     * 生成订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的user和cart对象
        User user = (User) req.getSession().getAttribute("user");
        Cart cart = (Cart) req.getSession().getAttribute("cart");

        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }
        //获取userId
        Integer userId = user.getId();
        //调用 orderService.createOrder(Cart,Userid); 生成订单
        String orderId = orderService.createOrder(cart, userId);

        //把orderId保存在session域中
        req.getSession().setAttribute("orderId", orderId);
        //重定向到/pages/cart/checkout.jsp
        resp.sendRedirect(req.getContextPath() + "/pages/cart/checkout.jsp");
    }

    /**
     * 展示所有的订单（超链接：我的订单）
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showMyOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取userId
        User user = (User) req.getSession().getAttribute("user");
        Integer userId = user.getId();
        //调用showMyOrders
        List<Order> orders = orderService.showMyOrders(userId);
        req.getSession().setAttribute("orders", orders);
        //3.重定向
        resp.sendRedirect(req.getContextPath() + "/pages/order/order.jsp");
    }

    /**
     * 发货
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void sendOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.调用showAllOrders
        List<Order> orders = orderService.showAllOrders();
        req.getSession().setAttribute("orders", orders);
        //2.获取请求的参数orderId
        String orderId = req.getParameter("orderId");
        //3.调用sendOrder();
        orderService.sendOrder(orderId);
        //4.重定向
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showAllOrders");
    }

    /**
     * 展示全部订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showAllOrders(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用showMyOrders
        List<Order> orders = orderService.showAllOrders();
        req.getSession().setAttribute("orders", orders);
        //3.重定向
        resp.sendRedirect(req.getContextPath() + "/pages/manager/order_manager.jsp");
    }

    /**
     * 展示订单详情
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void showOrderDetail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数orderId   orders中的属性status  m  order的username
        String orderId = req.getParameter("orderId");
        int status = WebUtils.parseInt(req.getParameter("status"), 0);
        int m = WebUtils.parseInt(req.getParameter("m"), 0);
        String username = req.getParameter("username");
        //2.调用showOrderDetail方法
        List<OrderItem> orderItems = orderService.showOrderDetail(orderId);
        //3.把订单项对象保存到session域中
        req.getSession().setAttribute("orderItems", orderItems);
        //4.重定向
        if (m == 1) {
            resp.sendRedirect(req.getContextPath() + "/pages/order/order_detail.jsp?status=" + status + "&m=" + 1 + "&orderId=" + orderId + "&username=" + username);
        } else {
            resp.sendRedirect(req.getContextPath() + "/pages/order/order_detail.jsp?status=" + status + "&orderId=" + orderId + "&username=" + username);
        }
    }

    /**
     * 签收订单
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void receiveOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        orderService.receiveOrder(orderId);
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showOrderDetail&status=" + 2 + "&orderId=" + orderId);
    }

    /**
     * 通过订单号查询用户名（将t_order与t_user连接连接查询）
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void queryUsernameByOrderId(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        String status = req.getParameter("status");
        String m = req.getParameter("m");
        Object username = orderService.queryUsernameByOrderId(orderId);
        resp.sendRedirect(req.getContextPath() + "/orderServlet?action=showOrderDetail&orderId=" + orderId + "&status=" + status + "&m=" + m + "&username=" + username);
    }
}
